#!/usr/bin/env python
#-*- coding:utf-8 -*-

RUN_LEVEL = ""
ROOT_PATH = ""
SETTINGS = {
    "development": "config.development",
    "product": "config.product",
}


def _imp_module(name, g={}, l={}):
    mod = name.split('.')
    if len(mod) == 1:
        return __import__(mod[0], g, l, [])
    name = mod[-1]
    mod = ".".join(mod[:-1])
    m = __import__(mod, g, l, [name])
    return getattr(m, name)


def get_app_settings():
    return _imp_module(SETTINGS.get(RUN_LEVEL, "development")).SETTINGS


def get_settings(name, default=""):
    return _imp_module(SETTINGS.get(
        RUN_LEVEL, "development")).OTHER_SETTINGS.get(name, default)


def set_database(name, engine):
    _imp_module(SETTINGS.get(RUN_LEVEL, "development")).DATABASES[name] = engine


def get_database(name="default"):
    return _imp_module(SETTINGS.get(
        RUN_LEVEL, "development")).DATABASES.get(name)


def set_database_session(name, session):
    _imp_module(SETTINGS.get(RUN_LEVEL, "development")).DATABASE_SESSIONS[name] = session


def get_database_session(name="default"):
    return _imp_module(SETTINGS.get(
        RUN_LEVEL, "development")).DATABASE_SESSIONS.get(name)
